package com.ews.onlineexchange.app;

import java.util.ArrayList;
import java.util.List;

import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.model.ServiceSupplier;
import com.ews.onlineexchange.model.Trade;
import com.ews.onlineexchange.service.EntityQuery;
import com.ews.onlineexchange.service.UserService;

/**
 * 中介机构查询本用户已经接受邀请的交易列表
 * @author admin
 *
 */
@Model
public class SupplierAppliedTradeListForApp extends EntityQuery<Trade> {
	private String app_userId;
	private String projectName;
	private String serialNumber;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public SupplierAppliedTradeListForApp() {
		super();
		setOrder("createDate DESC");
		
	}
	@Inject
	UserService userService;
	
	@Override
	protected List<Predicate> getRestrictions(CriteriaBuilder cb, Root<Trade> entity) {
		List<Predicate> restrictions = new ArrayList<Predicate>();
		restrictions.add(cb.equal(entity.get("delFlag"), DataEntity.DEL_FLAG_NORMAL));
		restrictions.add(cb.isTrue(entity.<Boolean>get("apply")));
		Join<Trade, ServiceSupplier> supplier = entity.join("supplier");
		restrictions.add(cb.equal(supplier.get("id"), app_userId));
		
		Join<Trade, Bulletin> bulletin = entity.join("bulletin");
		//11-09 针对直接选取 只有当公告审核成功后 才能查到数据
		restrictions.add(cb.isTrue(bulletin.<Boolean>get("auditResult")));
		//过滤已经取消的公告
		restrictions.add(cb.isFalse(bulletin.<Boolean>get("cancelled")));
		String projectName = (String)getParameterValues().get("projectName");
		if (!StringUtils.isBlank(projectName))
			restrictions.add(cb.like(bulletin.get("projectName"), getStringRestriction(projectName)));
		
		String serialNumber = (String)getParameterValues().get("serialNumber");
		if (!StringUtils.isBlank(serialNumber))
			restrictions.add(cb.like(bulletin.get("serialNumber"), getStringRestriction(serialNumber)));
		return restrictions;
	}

	public String getApp_userId() {
		return app_userId;
	}

	public void setApp_userId(String app_userId) {
		this.app_userId = app_userId;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getSerialNumber() {
		return serialNumber;
	}

	public void setSerialNumber(String serialNumber) {
		this.serialNumber = serialNumber;
	}

//	@Override
//	protected void fetch(Root<Trade> entity) {
//		// TODO Auto-generated method stub
//		entity.fetch("bulletin").fetch("trades");
////		super.fetch(entity);
//	}
	
	
}
